var util = require('../../utils/util.js');
var api = require('../../config/api.js');

Page({
  data: {
    newGoods:[],
    categoryList: [],
    currentCategory: {},
    currentSubCategoryList: {},
    scrollLeft: 0,
    scrollTop: 0,
    goodsCount: 0,
    scrollHeight: 0,
    currentSort: 'browse',
    currentSortOrder: 'desc',
    page: 1,
    size: 10,
    categoryId: 0,
    goodsList:[],
    totalPages: 1,
    isSearch: true,
  },
  onLoad: function(options) {
    this.getCatalog();
    this.goodsListS();
  },
  onPullDownRefresh() {
    wx.showNavigationBarLoading() //在标题栏中显示加载
    this.getCatalog();
    wx.hideNavigationBarLoading() //完成停止加载
    wx.stopPullDownRefresh() //停止下拉刷新
  },
  getCatalog: function() {
    //CatalogList
    let that = this;
    wx.showLoading({
      title: '加载中...',
    });
    util.request(api.CatalogList).then(function(res) {
      
      that.setData({
        categoryList: res.data.categoryList,
        currentCategory: res.data.currentCategory,
        currentSubCategoryList: res.data.currentSubCategory
      });

      wx.hideLoading();
    });
    util.request(api.GoodsCount).then(function(res) {
      that.setData({
        goodsCount: res.data.goodsCount
      });
    });

  },
  getCurrentCategory: function(id) {
    let that = this;
    util.request(api.CatalogCurrent, {
        id: id
      })
      .then(function(res) {
        that.setData({
          currentCategory: res.data.currentCategory,
          currentSubCategoryList: res.data.currentSubCategory
        });
      });
  },
  onReady: function() {
    // 页面渲染完成
  },
  onShow: function() {


  },
  onHide: function() {
    // 页面隐藏
  },
  onUnload: function() {
    // 页面关闭
  },
  switchCate: function(event) {
    var that = this;
    var currentTarget = event.currentTarget;
    if (this.data.currentCategory.id == event.currentTarget.dataset.id) {
      return false;
    }

    this.getCurrentCategory(event.currentTarget.dataset.id);
  },
  goodsListS(){
     // 页面显示
     var that = this;
     util.request(api.GoodsList, {
       page: that.data.page,
       size: that.data.size,
       order: that.data.currentSortOrder,
       sort: that.data.currentSort,
       categoryId: that.data.categoryId,
      //  isHot:
     }).then(function(res) {
       if (res.errno === 0) {
       
        var goodsListCS= that.data.goodsList.concat(res.data.goodsList)
        const map = new Map();
        const newArr = goodsListCS.filter(v => !map.has(v.id) && map.set(v.id, v));
         that.setData({
          goodsList: that.data.isSearch ? res.data.goodsList : newArr,
            filterCategory: res.data.filterCategoryList,
            totalPages: res.data.totalPages,
            isSearch: false
      });

         
       }
     });
 
  },
  onReachBottom:function() {
  
    if (this.data.totalPages > this.data.page) {
        this.setData({
          page: this.data.page + 1
      });
    } else {
      wx.showToast({
          title: '已经到底了!',
        icon: 'none',
        duration: 2000
      });
      return false;
    }
    this.goodsListS();
    },
    openSortFilter: function(event) {
      let currentId = event.currentTarget.id;
      let tmpSortOrder = 'asc';
     
      switch (currentId) {
        case 'categoryFilter':
          this.setData({
            categoryFilter: !this.data.categoryFilter,
            currentSortType: 'category',
            currentSort: 'add_time',
            currentSortOrder: 'desc',
            isSearch: true
          });
          break;
        case 'priceSort':
          if (this.data.currentSortOrder == 'asc') {
            tmpSortOrder = 'desc';
          }
          this.setData({
            currentSortType: 'price',
            currentSort: 'retail_price',
            currentSortOrder: tmpSortOrder,
            categoryFilter: false,
            isSearch: true
          });
  
          this.goodsListS();
          break;
        case 'salesSort':
          if (this.data.currentSortOrder == 'asc') {
            tmpSortOrder = 'desc';
          }
          this.setData({
            currentSortType: 'sales',
            currentSort: 'sales',
            currentSortOrder: tmpSortOrder,
            categoryFilter: false,
            isSearch: true
          });
          this.goodsListS();
          break;
        default:
          //综合排序
          this.setData({
            currentSortType: 'default',
            currentSort: 'add_time',
            currentSortOrder: 'desc',
            categoryFilter: false,
            categoryId: 0,
            isSearch: true
          });
          this.goodsListS();
      }
    },
    onPageScroll:function(e){
      if(e.scrollTop < 20){
        this.setData({
          page:1
        })
      }
    },
    
})